﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UI;
using Base.Data;
using Base.Custom;
using Base.Extensions;
public class MissionPanel : BasePanel {
    public Object missionItem;
    private List<Transform> content;
    private SingleObject singleobj;
    private Transform container;
    // Use this for initialization
    void Awake () {
        content = new List<Transform>();
        container = transform.Find("Scroll View/Viewport/Content");
    }
	
	// Update is called once per frame
	void Update () {
        if (singleobj != FreeCamera.freecamera.singleobj)
        {
            OnEnter();
        }
	}
    public override void OnEnter()
    {
        if (FreeCamera.freecamera.singleobj==null)
        {

        }
        else
        {
            gameObject.SetActive(true);
            if (singleobj != FreeCamera.freecamera.singleobj)
            {
                singleobj = FreeCamera.freecamera.singleobj;
                for (int i = 0; i < singleobj.msg.property.Length; i++)
                {
                    if (i<content.Count)
                    {
                        content[i].Find("Key/Text").SetText(singleobj.msg.property[i].name);
                        content[i].Find("Value/Text").SetText(singleobj.msg.property[i].value);
                    }
                    else
                    {
                        GameObject miss = Instantiate(missionItem) as GameObject;
                        miss.SetActive(true);
                        miss.transform.parent = container;
                        miss.transform.localPosition = Vector3.zero;
                        miss.transform.Find("Key/Text").SetText(singleobj.msg.property[i].name);
                        miss.transform.Find("Value/Text").SetText(singleobj.msg.property[i].value);
                        content.Add(miss.transform);
                    }
                }
                if (content.Count>singleobj.msg.property.Length)
                {
                    for (int i = singleobj.msg.property.Length; i < content.Count; i++)
                    {
                        DestroyImmediate(content[i].gameObject);
                    }
                    content.RemoveRange(singleobj.msg.property.Length, content.Count- singleobj.msg.property.Length);
                }
            }
        }
    }
    public override void OnExit()
    {
        gameObject.SetActive(false);
    }

    public void offOnClick()
    {
        UIManager.Instance.PopPanel();
    }

}
